//
//  main.c
//  socket通信
//
//  Created by 解楚豪 on 2018/7/30.
//  Copyright © 2018年 解楚豪. All rights reserved.
//

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, const char * argv[]) {
    int tcp_Socket = socket(AF_INET, SOCK_STREAM, 0);
    int udp_Socket = socket(AF_INET, SOCK_DGRAM, 0);
    
    //服务端
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //清空数组
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip地址
    serv_addr.sin_port = htons(1234);//端口号
    bind(tcp_Socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    
    //监听状态
    listen(tcp_Socket, SOMAXCONN);
    unsigned int sSizeof = sizeof(serv_addr);
    //接口
    int jieshouCliect = accept(tcp_Socket, (struct sockaddr *)&serv_addr, &sSizeof);
    char buffer[100];
    long strLen = read(jieshouCliect, buffer, 100);//读取
    send(jieshouCliect, buffer, strLen, 0);//发送
    //关闭套接字
    close(jieshouCliect);
    close(tcp_Socket);
//    closesocket(servSock);
    
    //客户端
    struct sockaddr_in cliect_addr;
    memset(&cliect_addr, 0, sizeof(cliect_addr));  //清空数组
    cliect_addr.sin_family = AF_INET;
    cliect_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip地址
    cliect_addr.sin_port = htons(1234);//端口号
    connect(udp_Socket, (struct sockaddr *)&cliect_addr, sizeof(cliect_addr));
    printf("Hello, World!\n");
    return 0;
}
